package com.ohs.bll.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import nl.captcha.Captcha;

import org.apache.struts2.ServletActionContext;

import com.ohs.fo.action.SMSGateway;

public class CommonUtil {
	public static boolean checkSMSCaptcha(String phone, String captcha) {
		phone = phone.substring(phone.length() - 9, phone.length());
		String code = SMSGateway.verifyCodes.get(phone);
		if (code != null && code.equalsIgnoreCase(captcha)) {
			return true;
		}
		return false;
	}
	
	public static void removeCaptcha(String phone) {
		SMSGateway.verifyCodes.remove(phone);
	}
	
	public static boolean checkCaptcha(HttpSession session, String captcha) {
		HttpSession ss = ServletActionContext.getRequest().getSession();
		if (ss != null) {
			Captcha capt = (Captcha) ss.getAttribute(Captcha.NAME);
			if (capt != null && captcha != null) {
				if (capt.isCorrect(captcha)) {
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}
	
	public static String getUsername(HttpServletRequest request) {
		return (String) request.getSession().getAttribute(Constant.USER_NAME);
	}
}
